//
//  LMHomeDetailTableViewController.m
//  EPA
//
//  Created by lanou3g on 16/4/20.
//  Copyright © 2016年 liman. All rights reserved.
//

#import "LMHomeDetailTableViewController.h"
#import "LMNetWorkRequestManager.h"
#import "LMHomeListCell.h"
#import "LMHomeListModel.h"
#import "LORefresh.h"
#import "UIImage+ImageEffects.h"
#import "LMReadController.h"
@interface LMHomeDetailTableViewController ()
///存储模型数据的数组
@property (nonatomic, strong) NSMutableArray *listModelArray;
///请求内容起始条数
@property (nonatomic, strong) NSString *start;
///Header属性
@property (nonatomic , strong) LORefresh *headerRefresh;
@end

@implementation LMHomeDetailTableViewController

- (void)dealloc{
//    gifHeader根据不同类型 进行释放观察者

    [self.tableView removeObserver:[self.tableView gifHeader] forKeyPath:@"contentOffset"];

    [self.tableView removeObserver:[self.tableView defaultFooter] forKeyPath:@"contentOffset"];
    [self.tableView removeObserver:[self.tableView defaultFooter] forKeyPath:@"contentSize"];
   
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.listModelArray = [NSMutableArray array];
    self.navigationItem.title = self.model.name;
    self.start = @"0";
    [self loadDataFromServerType:[NSString stringWithFormat:@"%ld",self.typeID]Start:self.start];
    
    self.tableView.separatorStyle = UITableViewCellSelectionStyleNone;
    UIImage *img = [UIImage imageNamed:@"IMG_0106.JPG"];
    self.view.backgroundColor = [UIColor colorWithPatternImage:[img applyLightEffect]];
    
//    [self loadDataFromServerType:@"" Start:@""];
    [self.tableView registerNib:[UINib nibWithNibName:@"LMHomeListCell" bundle:nil] forCellReuseIdentifier:@"LMHomeListCell"];
    

    [self addRefresh];
    
}
#pragma mark - 添加下拉刷新 上提加载更多
- (void)addRefresh{
    /*
     添加的这个第三方是一个利用的观察者，
     1.创建：网络请求
     2.手动停止
     3.释放观察者
     */
///******************添加下拉刷新
    __weak typeof(self) weakSelf = self;
    [self.tableView addRefreshWithRefreshViewType:LORefreshViewTypeHeaderGif refreshingBlock:^{
        //        首先移除
        [weakSelf.listModelArray removeAllObjects];
        [weakSelf.tableView reloadData];
        //        网络指示
        [weakSelf loadDataFromServerType:[NSString stringWithFormat:@"%ld",weakSelf.typeID ] Start:@"0"];
    }];
    
    self.headerRefresh = [self.tableView gifHeader];
    [self.headerRefresh setGifName:@"wangnima"];
    
///*****************添加上提加载更多
    [self.tableView addRefreshWithRefreshViewType:LORefreshViewTypeFooterDefault refreshingBlock:^{
        NSInteger count = weakSelf.listModelArray.count;
        weakSelf.start = [NSString stringWithFormat:@"%ld",count];
        //        网络请求
        [weakSelf loadDataFromServerType:[NSString stringWithFormat:@"%ld",weakSelf.typeID ] Start:weakSelf.start];
    }];
    
    
}

#pragma mark - 网络请求方法
- (void)loadDataFromServerType:(NSString *)type Start:(NSString *)strat{
    NSDictionary *params = [NSDictionary dictionary];
    if ([UserInfoManager shareManager].auth) {
        params =@{@"deviceid":@"63A94D37-33F9-40FF-9EBB-481182338873",@"typeid":[NSString stringWithFormat:@"%ld",(long)self.typeID],@"client":@"1",@"sort":@"addtime",@"limit":@10,@"version":@"3.0.2",@"auth":[UserInfoManager shareManager].auth,@"start":strat} ;
    }else{
        params = @{@"deviceid":@"63A94D37-33F9-40FF-9EBB-481182338873",@"typeid":[NSString stringWithFormat:@"%ld",(long)self.typeID],@"client":@"1",@"sort":@"addtime",@"limit":@10,@"version":@"3.0.2",@"auth":@"",@"start":strat} ;
    }
    
    [LMNetWorkRequestManager requestURL:[NSURL URLWithString:READDETAILLIST_URL] Method:LM_POST Params:params Result:^(NSData *data, NSURLResponse *response, NSError *error) {
        NSLog( @"%@",[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]);
        if (data) {
            NSDictionary *jsonDict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
            NSDictionary *dataDict = jsonDict[@"data"];
            for (NSDictionary *item in dataDict[@"list"]) {
                LMHomeListModel *model = [[LMHomeListModel alloc]init];
                [model setValuesForKeysWithDictionary:item];
                [self.listModelArray addObject:model];
                
            }
            dispatch_async(dispatch_get_main_queue(), ^{
                  [self.tableView reloadData ];
//                手动调用停止刷新
                [[self.tableView gifHeader] endRefreshing];
                [[self.tableView defaultFooter] endRefreshing];
            });
          
        }
        
    }];
 
}


#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.listModelArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    LMHomeListCell *cell = [tableView dequeueReusableCellWithIdentifier:@"LMHomeListCell" ];
    cell.model =  self.listModelArray[indexPath.row];
    cell.backgroundColor = [UIColor clearColor];

    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 200;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    LMReadController *readVC = [[LMReadController alloc]init];
    LMHomeListModel *model = self.listModelArray[indexPath.row];
    readVC.model = model;
    [self.navigationController pushViewController:readVC animated:YES];
    
}



@end
